package com.lqg.bookLibrary.common.util;

import java.util.Random;

/**
 * 获取随机字符串
 * */
public class RandomStrings {

    /**默认随机字符长度* */
    private  static final int DEFAULT_LENGTH = 6;
    /**规定取随机字符的范围*/
    private static final String DEFAULT_STR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    public static String getRandomString(){
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<DEFAULT_LENGTH;i++){
            int number=random.nextInt(DEFAULT_STR.length());
            sb.append(DEFAULT_STR.charAt(number));
        }
        return sb.toString();
    }


    /**
     * @param length：随机字符串长度
     * @Param str： 取随机字符串的范围
     * */
    public String getRandomString(int length, String str){
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < length; i++){
            int number = random.nextInt(str.length());
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }
}
